package com.fengwan.wastesort.ui.fragment

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.LayoutRes
import androidx.appcompat.widget.AppCompatTextView
import androidx.cardview.widget.CardView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.fengwan.wastesort.R
import com.xuexiang.xui.utils.ResUtils

/**
 * @author : Silence丶J
 * @time : 2019/7/18
 * @description :
 */
open abstract class BaseWasteFragment : Fragment() {

    protected fun getItemHeader(@LayoutRes resource: Int, recycler: RecyclerView): View {
        return layoutInflater.inflate(resource, recycler.parent as ViewGroup, false)
    }

    protected fun setItemHeaderContent(
        view: View, @ColorRes viewBgColor: Int,
        title: CharSequence, @DrawableRes titleImage: Int,
        subtitle: CharSequence
    ) {
        view.findViewById<CardView>(R.id.card_view)
            .setCardBackgroundColor(ResUtils.getColor(viewBgColor))
        view.findViewById<AppCompatTextView>(R.id.tv_title).setCompoundDrawablesWithIntrinsicBounds(
            null,
            null,
            ResUtils.getDrawable(titleImage),
            null
        )
        view.findViewById<AppCompatTextView>(R.id.tv_title).text = title
        view.findViewById<AppCompatTextView>(R.id.tv_subtitle).text = subtitle
    }

    private var isHint = false
    private var isView = false

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        isView = true
        initView(view)
        initListener()
        loadData()
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        if (isVisibleToUser) {
            isHint = true
        }
        super.setUserVisibleHint(isVisibleToUser)
    }

    abstract fun loadData(_id: Int = 0, type: Int = 0)

    abstract fun initListener()

    abstract fun initView(view: View)

}